package com.ducquoc.examreviewdegreea1;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalFloat;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.widget.Toast;

public class DocCirAsyncTask extends
		AsyncTask<ArrayList<DocPojo>, Void, ArrayList<DocPojo>> {
	ArrayList<DocPojo> arrAsk = new ArrayList<DocPojo>();
	final String URL = "http://ltddhitc12hcb.somee.com/ducquocservice.asmx?WSDL";
	private DocLoadDataActivity activity;
	private DocDatabase database;
	private int version = 1;
	String currentDate = "", currentVersion = "", currentStatus = "null";

	public DocCirAsyncTask(DocLoadDataActivity activity) {
		this.activity = activity;
		database = new DocDatabase(activity);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		Toast.makeText(activity, "Đang tải dữ liệu!", Toast.LENGTH_LONG).show();

		// Doc du lieu de lay version cua CSDL tu file test_database.txt
		DocReadWriteFile docRWFile = new DocReadWriteFile();
		ArrayList<String> arrTestDbs = docRWFile.ReadFile();

		// Gan du lieu co trong ArrayList vao cac bien
		currentDate = arrTestDbs.get(0).toString();
		currentVersion = arrTestDbs.get(1).toString();
		version = Integer.parseInt(currentVersion);
		currentStatus = arrTestDbs.get(2).toString();
	}

	@Override
	protected ArrayList<DocPojo> doInBackground(ArrayList<DocPojo>... arg0) {
		try {
			final String NAMESPACE = "http://tempuri.org/";
			final String METHOD_NAME = "getListCir";
			final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);
			// Neu truyen so thuc tren mang bat buoc phai dang ky MarshalFloat
			// neu khong se bi bao loi
			MarshalFloat marshal = new MarshalFloat();
			marshal.register(envelope);

			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			androidHttpTransport.call(SOAP_ACTION, envelope);
			// Get Array Catalog into soapArray
			SoapObject soapArray = (SoapObject) envelope.getResponse();
			arrAsk.clear();
			// Vong lap duyet qua tung dong du lieu
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				// (SoapObject) soapArray.getProperty(i) get item at position i
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);

				DocPojo question = new DocPojo();
				// Map dung theo ten cot tren webservices
				String _idTemp = soapItem.getProperty("id").toString();
				int _id = Integer.parseInt(_idTemp);
				question.setId(_id);
				String _ask = soapItem.getProperty("ask").toString();
				question.setAsk(_ask);
				String _img = soapItem.getProperty("img").toString();
				question.setAsk(_img);
				String _ans1 = soapItem.getProperty("ans1").toString();
				question.setAns1(_ans1);
				String _ans2 = soapItem.getProperty("ans2").toString();
				question.setAns2(_ans2);
				String _ans3 = soapItem.getProperty("ans3").toString();
				question.setAns3(_ans3);
				String _ans4 = soapItem.getProperty("ans4").toString();
				question.setAns4(_ans4);
				String _trueans = soapItem.getProperty("trueans").toString();
				question.setTrueans(_trueans);
				String _hideTemp = soapItem.getProperty("hide").toString();
				int _hide = Integer.parseInt(_hideTemp);
				question.setHide(_hide);

				database.open(version);
				database.createDataCircumstance(_id, _ask, _img, _ans1, _ans2,
						_ans3, _ans4, _trueans, _hide);
				database.close();

				arrAsk.add(question);
			}
		} catch (Exception e) {
			
		}
		return null;
	}

	@Override
	protected void onPostExecute(ArrayList<DocPojo> result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		Toast.makeText(activity, "Đang tải dữ liệu!!!", Toast.LENGTH_LONG)
				.show();

		//activity.setList3(arrAsk);
	}
}
